﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dqs_DAL
{
	public class Class_DAL
	{
		#region 教室部分
		/// <summary>
		/// 插入教室信息
		/// </summary>
		/// <param name="classes"></param>
		/// <returns></returns>
		public bool InsertClassInfo(ClassInfo classes)
		{
			using (MyDbContext db = new MyDbContext())
			{
				if (classes == null)
					return false;
				db.ClassInfos.Add(classes);
				db.SaveChanges();
			}
			return true;
		}

		/// <summary>
		/// 通过Id修改教室信息
		/// </summary>
		/// <param name="classNew"></param>
		public bool UpdateClassInfoById(ClassInfo classNew)
		{
			using (MyDbContext db = new MyDbContext())
			{
				var classOld = db.ClassInfos.Where(x => x.Id == classNew.Id).FirstOrDefault();
				if (classOld == null)
				{
					return false;
				}
				classOld.Course = classNew.Course;
				classOld.SeatList = classNew.SeatList;
				classOld.ClassName = classNew.ClassName;
				classOld.IsMultimedia = classNew.IsMultimedia;
				classOld.IsOccupied = classNew.IsOccupied;
				classOld.MaxCount = classNew.MaxCount;
				db.SaveChanges();
			}
			return true;
		}

		/// <summary>
		/// 通过Id删除教室信息
		/// </summary>
		/// <param name="classId"></param>
		/// <returns></returns>
		public bool DeleteClassInfoById(int classId)
		{
			using (MyDbContext db = new MyDbContext())
			{
				var classes = db.ClassInfos.FirstOrDefault(s => s.Id == classId);
				if (classes == null)
				{
					return false;
				}
				db.ClassInfos.Remove(classes);
				db.SaveChanges();
			}
			return true;
		}

		/// <summary>
		/// 查询所有教室信息
		/// </summary>
		/// <returns></returns>
		public List<ClassInfo> QueryClassInfo()
		{
			using (MyDbContext db = new MyDbContext())
			{
				var classes = db.ClassInfos.ToList();
				return classes;
			}
		}



		#endregion
	}
}
